﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Decoration;
using TomNet.IMSC.Core.Contracts.QandA;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.IMSC.Model.DbModels.QandA;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.ApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.API
{
    [Description("API-动态列表")]
    [ApiAuthentication]
    public class DynamicInterfaceController : BaseApiController
    {
        public IDynamicModelContract DynamicModelContract { get; set; }
        public IDynamicPropertiesModelContract DynamicPropertiesModelContract { get; set; }
        public IDynamicCheapRoomModelContract DynamicCheapRoomModelContract { get; set; }
        public IDynamicDecorationModelContract DynamicDecorationModelContract { get; set; } 
        public IImagesContract ImagesContract { get; set; }
        #region 新房
        [Description("API-新房动态列表-查询")]
        [AllowAnonymous]
        public IHttpActionResult ttGetDynamicInterfaceListNewHouse([FromBody]DynamicInterfaceModel entity)
        {
            var query = from d in DynamicModelContract.TrackEntities.Where(m => !m.IsDeleted)
                         join dp in DynamicPropertiesModelContract.TrackEntities
                         on d.Id equals dp.DynamicID
                         where dp.BuildingsID == entity.BuildingID
                         select new DynamicInterfaceListModel
                         {
                             Id = d.Id,
                             Title = d.Title,
                             Subject = d.Subject,
                             ReleaseTime = d.ReleaseTime,
                             ImagePathList = (from img in ImagesContract.Entities
                                          where img.GroupID == 10 && img.ImageType == 1 && d.Id == img.InterfixID
                                          select img.ImagePath + img.FileName).ToList()
                         };


            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.ReleaseTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : query;

            var result = new AjaxResultRow("成功", AjaxResultType.Success, query.ToList(),total); 
            return Token(result, "");
        }
        #endregion

        #region 低价房
        [Description("API-低价房动态列表-查询")]
        [AllowAnonymous]
        public IHttpActionResult ttGetDynamicInterfaceListLowPriceHouse([FromBody]DynamicInterfaceModel entity)
        {
            var query = from d in DynamicModelContract.TrackEntities.Where(m => !m.IsDeleted)
                         join dp in DynamicCheapRoomModelContract.TrackEntities
                         on d.Id equals dp.DynamicID
                         where dp.LowPriceHousesID == entity.LowPriceHousesID
                         select new DynamicInterfaceListModel
                         {
                             Id = d.Id,
                             Title = d.Title,
                             Subject = d.Subject,
                             ReleaseTime = d.ReleaseTime,
                             ImagePathList = (from img in ImagesContract.Entities
                                          where img.GroupID == 10 && img.ImageType == 1 && d.Id == img.InterfixID
                                          select img.ImagePath + img.FileName).ToList()
                         };


            //数据总数
            var total = query.Count(); 
            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.ReleaseTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : query;

            var result = new AjaxResultRow("成功", AjaxResultType.Success, query.ToList(), total);
            return Token(result, "");
        }
        #endregion

        #region 装修
        [Description("API-装修公司动态列表-查询")]
        [AllowAnonymous]
        public IHttpActionResult ttGetDynamicInterfaceListDecorationCompany([FromBody]DynamicInterfaceModel entity)
        {
            var query = from d in DynamicModelContract.TrackEntities.Where(m => !m.IsDeleted)
                         join dtc in DynamicDecorationModelContract.TrackEntities
                         on d.Id equals dtc.DynamicID
                         where dtc.DecorationID == entity.DecorationCompanyID
                         select new DynamicInterfaceListModel
                         {
                             Id = d.Id,
                             Title = d.Title,
                             Subject = d.Subject,
                             ReleaseTime = d.ReleaseTime,
                             ImagePathList = (from img in ImagesContract.Entities
                                          where img.GroupID == 10 && img.ImageType == 1 && d.Id == img.InterfixID
                                          select img.ImagePath + img.FileName).ToList()
                         };

            //数据总数
            var total = query.Count();


            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.ReleaseTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : query;

            var result = new AjaxResultRow("成功", AjaxResultType.Success, query, total);
            return Token(result, "");
        }
        #endregion

        #region 所有
        public IHttpActionResult ttGetDynamicInterfaceList([FromBody]DynamicInterfaceModel entity)
        {
            var query = from d in DynamicModelContract.TrackEntities.Where(m => !m.IsDeleted)
                        select new DynamicInterfaceListModel
                        {
                            Id = d.Id,
                            Title = d.Title,
                            Subject = d.Subject,
                            ReleaseTime = d.ReleaseTime,
                            ImagePathList = (from img in ImagesContract.Entities
                                         where img.GroupID == 10 && img.ImageType == 1 && d.Id == img.InterfixID && img.IsDeleted != true
                                         select img.ImagePath + img.FileName).ToList()
                        };

            //数据总数
            var total = query.Count();


            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.ReleaseTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : query;

            var result = new AjaxResultRow("成功", AjaxResultType.Success, query, total);
            return Token(result, "");
        }
        #endregion

        #region 动态Model
        public IHttpActionResult ttGetDynamicModel([FromBody]DynamicModel entity)
        {
            DynamicModel d = DynamicModelContract.GetByKey(entity.Id);
            DynamicInterfaceListModel dm = new Models.ApiParams.DynamicInterfaceListModel();
            dm.Id = d.Id;
            dm.Title = d.Title;
            dm.Subject = d.Subject;
            dm.ReleaseTime = d.ReleaseTime;
            dm.ImagePathList = (from img in ImagesContract.Entities
                            where img.GroupID == 10 && img.ImageType == 1 && d.Id == img.InterfixID && img.IsDeleted != true
                            select img.ImagePath + img.FileName).ToList();
            var result = new AjaxResult("成功", AjaxResultType.Success, dm);
            return Token(result, "");
        }
        #endregion
    }
}